package com.rr.graphql.resolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.graphql.dgs.*;
import com.netflix.graphql.dgs.context.DgsContext;
import com.rr.constant.SexEnum;
import com.rr.dto.Employee;
import com.rr.graphql.context.MyCustomContext;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author RR
 * @date 2023/6/11 17:27
 */
@DgsComponent
@Slf4j
public class EmployeeQueryResolver {

    int index = 0;

    @DgsQuery
    public List<Employee> employees() {
        List<Employee> employees = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Employee employee = new Employee();
            employee.setId(String.valueOf(index++));
            employee.setName("zcr" + index);
            employee.setSex(SexEnum.MALE);
            employee.setBirthday(LocalDateTime.now());
            employees.add(employee);
        }
        return employees;
    }

    /**
     * 每一个部门ID，对应一个List<Employee>
     * 所以响应体是List<List<Employee>>
     * @param departmentIds
     * @return
     */
    public List<List<Employee>> employeeQueryByDepartmentIds(List<String> departmentIds) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        List<List<Employee>> employees = new ArrayList<>();
        log.info("批量查询部门员工信息，部门ID列表:{}", departmentIds);
        for (String id : departmentIds) {
            List<Employee> employeeList = new ArrayList<>();
            Employee employee = new Employee();
            employee.setId(id);
            employee.setName("zcr");
            employee.setSex(SexEnum.MALE);
            employee.setBirthday(LocalDateTime.now());
            employeeList.add(employee);
            employees.add(employeeList);
        }
        return employees;

        // 反例
//        for (int i = 0; i < departmentIds.size(); i++) {
//            List<Employee> employeeList = new ArrayList<>();
//            Employee employee = new Employee();
//            employee.setId(String.valueOf(i));
//            employee.setName("zcr");
//            employee.setSex(SexEnum.MALE);
//            employee.setBirthday(LocalDateTime.now());
////            employee.setDepartmentId(id);
//            employeeList.add(employee);
//            employees.add(employeeList);
//        }
//        List<List<Employee>> employeesRev = new ArrayList<>();
//        for (int i = employees.size()-1; i >=0; i--) {
//            employeesRev.add(employees.get(i));
//        }
//        return employeesRev;
    }
    @DgsQuery
    public Employee employee(@InputArgument String id, DgsDataFetchingEnvironment env) {
        MyCustomContext customContext = DgsContext.getCustomContext(env);
        log.info("上下文信息：{}", customContext);
        Employee employee = new Employee();
        employee.setId(id);
        employee.setName("zcr");
        employee.setSex(SexEnum.MALE);
        employee.setBirthday(LocalDateTime.now());
        return employee;
    }

    @DgsData(parentType = "Employee",field = "departmentId")
    public String departmentId(DgsDataFetchingEnvironment env){
        Employee source = env.getSource();
        String employeeId = source.getId();
        // 根据员工ID查部门信息
        log.info("查询员工部门信息，员工ID:{}", employeeId);
        return "000" + employeeId;
    }

}